package jehc.djshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BasePage;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseSearch;
import jehc.djshi.common.entity.OauthAccountEntity;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.sys.model.XtUnit;
import jehc.djshi.sys.service.XtUnitService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 商品(产品)单位
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtUnit")
@Api(value = "商品(产品)单位API",tags = "商品(产品)单位API",description = "商品(产品)单位API")
public class XtUnitController extends BaseAction {

	@Resource
	private XtUnitService xtUnitService;
	
	/**
	* 查询商品(产品)单位列表并分页
	* @param baseSearch
	*/
	@PostMapping(value="/list")
	@Auth(value = "/xtUnit/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询商品(产品)单位列表并分页", notes="查询商品(产品)单位列表并分页")
	public BasePage<List<XtUnit>> getXtUnitListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<XtUnit> xtUnitList = xtUnitService.getXtUnitListByCondition(condition);
		for(XtUnit xtUnit : xtUnitList){
			if(!StringUtil.isEmpty(xtUnit.getCreateId())){
				OauthAccountEntity createBy = getAccount(xtUnit.getCreateId());
				if(null != createBy){
					xtUnit.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(xtUnit.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(xtUnit.getUpdateId());
				if(null != modifiedBy){
					xtUnit.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<XtUnit> page = new PageInfo<XtUnit>(xtUnitList);
		return outPageBootStr(page,baseSearch);
	}
	
	/**
	* 查询单个商品(产品)单位
	* @param id
	*/
	@GetMapping(value="/get/{id}")
	@Auth(value = "/xtUnit/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个商品(产品)单位", notes="查询单个商品(产品)单位")
	public BaseResult<XtUnit> getXtUnitById(@PathVariable("id")String id){
		XtUnit xtUnit = xtUnitService.getXtUnitById(id);
		if(!StringUtil.isEmpty(xtUnit.getCreateId())){
			OauthAccountEntity createBy = getAccount(xtUnit.getCreateId());
			if(null != createBy){
				xtUnit.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(xtUnit.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(xtUnit.getUpdateId());
			if(null != modifiedBy){
				xtUnit.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(xtUnit);
	}
	
	/**
	* 添加
	* @param xtUnit
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个商品(产品)单位", notes="创建单个商品(产品)单位")
	public BaseResult addXtUnit(@RequestBody XtUnit xtUnit){
		int i = 0;
		if(null != xtUnit){
			xtUnit.setId(toUUID());
			xtUnit.setCreateId(getXtUid());
			xtUnit.setCreateTime(getDate());
			i=xtUnitService.addXtUnit(xtUnit);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 修改
	* @param xtUnit
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个商品(产品)单位", notes="编辑单个商品(产品)单位")
	public BaseResult updateXtUnit(@RequestBody XtUnit xtUnit){
		int i = 0;
		if(null != xtUnit){
			xtUnit.setUpdateId(getXtUid());
			xtUnit.setUpdateTime(getDate());
			i=xtUnitService.updateXtUnit(xtUnit);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除商品(产品)单位", notes="删除单个商品(产品)单位")
	public BaseResult delXtUnit(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=xtUnitService.delXtUnit(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
